//
//  HELPayResultVC.m
//  NursingAssociation
//
//  Created by wsj2015 on 2017/3/6.
//  Copyright © 2017年 wsj2015. All rights reserved.
//

#import "HELPayResultVC.h"
#import "BFPaperButton.h"
#import "NSObject+CountDown.h"
@interface HELPayResultVC ()
@property (weak, nonatomic) IBOutlet UILabel *titleL;
@property (weak, nonatomic) IBOutlet UIImageView *result_img;
@property (weak, nonatomic) IBOutlet UILabel *big_title;
@property (weak, nonatomic) IBOutlet UILabel *reson;

@property (weak, nonatomic) IBOutlet BFPaperButton *leftBtn;

@property (weak, nonatomic) IBOutlet BFPaperButton *rightBtn;
@property (weak, nonatomic) IBOutlet UILabel *countL;

@property (nonatomic,  strong) dispatch_source_t timer;

@property (nonatomic,  assign) NSInteger isSucess;
@property (nonatomic,  assign) BOOL isFromCourse;

@end

@implementation HELPayResultVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    
    self.view.backgroundColor  =kTableView_bg;
    [self resetViewPayresult:self.isSucess isFromCourse:self.isFromCourse];
    // GCD定时器
    
    NSTimeInterval period = 1.0; //设置时间间隔
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    dispatch_source_set_timer(_timer, dispatch_walltime(NULL, 0), period * NSEC_PER_SEC, 0); //每秒执行
    // 事件回调
    __block NSInteger i = 3;
    dispatch_source_set_event_handler(_timer, ^{
        //
        
        dispatch_async(dispatch_get_main_queue(), ^{
            self.countL.text = [NSString stringWithFormat:@"(%@秒后)自动返回",@(i--)];
            if (i == 0) {
                dispatch_cancel(self.timer);
            }
        });
    });
    
    dispatch_resume(self.timer);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)dealloc{

    
}




- (IBAction)leftBtnAction:(id)sender {
    
    dispatch_cancel(self.timer);
    if (self.clickBlock) {
        self.clickBlock(0);
    }
}
- (IBAction)rightBtnAction:(id)sender {
     dispatch_cancel(self.timer);
    if (self.clickBlock) {
        self.clickBlock(1);
    }
}
- (void)setresult:(NSInteger)isSucess isFromCourse:(BOOL)isFromCourse{
    self.isSucess = isSucess;
    self.isFromCourse = isFromCourse;
}

- (void)resetViewPayresult:(NSInteger)isSucess isFromCourse:(BOOL)isFromCourse{
 
    self.isSucess = isSucess;
    if (isSucess == 1) {
        
        
        self.result_img.image = [UIImage imageNamed:@"支付成功 copy"];
        self.leftBtn.backgroundColor = kPinchColor;
        self.big_title.text = @"支付成功!";
        self.reson.hidden  =YES;
        [self.leftBtn setTitle:@"返回课程" forState:UIControlStateNormal];
        if (isFromCourse) {
                 [self.leftBtn setTitle:@"返回课程" forState:UIControlStateNormal];
        }else{
                [self.leftBtn setTitle:@"返回订单" forState:UIControlStateNormal];
        }
   
        
        
    }else if (isSucess == -1){ //不支持支付
      
       self.big_title.text = @"不支持在线支付";
       self.reson.hidden  =YES;
        if (isFromCourse) {
            [self.leftBtn setTitle:@"返回课程" forState:UIControlStateNormal];
        }else{
            [self.leftBtn setTitle:@"返回订单" forState:UIControlStateNormal];
        }
    }else if(isSucess == 0){
        self.countL.hidden = YES;
       
    }
 

}



@end
